/*
 * eleConfig.js
 * 要素配置
 */
;
(function (call) {
    var main = {
        router: {
            getElementList:"/hjimg/fxSet/getFxSetInfo",
            //getElementList: "/business/common/data/autostat.json",
            updateElementData: "/hjimg/fxSet/updateFxSetInfo"
        },
        init: function () {
            this.getElementList();
            this.bindEvt();
        },
        bindEvt: function () {
            var that = this;
            //绑定取消按钮
            $(".cancelConfig").unbind("click").bind("click", function () {
                $("#eleConfig").removeClass("active");
                CafeJS.ModalBoxClose("eleConfig");
            })
            //绑定确定按钮
            $(".submitConfig").unbind("click").bind("click", function () {
                that.updateElementData();
            })
        },
        //获取最新数据
        getElementList: function () {
            var that = this;
            $.ajax({
                url: this.router.getElementList,
                type: 'post',
            }).done(function (res) {
                if (res.result.data && res.isSuccessful) {
                    var eleData = res.result.data;
                    that.initView(eleData);
                }
            })
        },
        //初始化页面结构
        initView: function (data) {
            var parentObj = $(".eleConfig_content >div");
            parentObj.html("");
            for (var i = 0; i < data.length; i++) {
                var product = data[i];
                var productObj = $("<div class='product-item'><div class='product-title'>" + product.name + "</div></div>");
                var eleObj = $("<ul></ul>");
                parentObj.append(productObj);
                productObj.append(eleObj);
                for (var j = 0; j < product.elements.length; j++) {
                    var ele = product.elements[j];
                    var liObj = $("<li><span>" + ele.name + "</span></li>");
                    liObj.attr("name", ele.element);
                    if (ele.is_sel === "1") {
                        liObj.addClass("active");
                    }
                    eleObj.append(liObj);
                    liObj.unbind("click").bind("click", function () {
                        $(this).toggleClass("active");
                    })
                }
            }
        },
        //更新要素配置
        updateElementData: function () {
            var that = this;
            var elementsArr = [];
            $(".eleConfig_content .active").each(function () {
                elementsArr.push($(this).attr("name"));
            })
            $.ajax({
                url: this.router.updateElementData,
                type: 'post',
                data: {
                    "elements": elementsArr.join(",")
                }
            }).done(function (res) {
                if (res.isSuccessful) {
                    alert("设置成功");
                    $("#eleConfig").removeClass("active");
                    CafeJS.ModalBoxClose("eleConfig");
                    that.refreshPage();
                }
            })
        },
        //刷新自动站页面
        refreshPage: function () {
            var that = this;
            $(".autostat .product-list li.active").each(function () {
                $(this).click();
            })
            if ($("#childMenu .autostat").hasClass("active")) {
                CafeJS.autostat.init();
            } else {
                CafeJS.ModalBoxClose("autostat");
            }
        }
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
});